package com.woorigil.social.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.woorigil.social.controller.BaseController;
import com.woorigil.social.domain.category.Category;
import com.woorigil.social.domain.category.CategoryCondition;
import com.woorigil.social.domain.category.CategoryDao;

@Controller
@RequestMapping("/category")
public class CategoryController extends BaseController {
	@Autowired CategoryDao categoryDao;
	
//    @InitBinder
//    protected void initBinder(WebDataBinder binder) {
//        binder.setValidator(new CategoryValidator());
//    }	
	
	@RequestMapping("/form")
	public ModelAndView form(@RequestParam(defaultValue="0") int id) {
		Category category = categoryDao.select(id);
		ModelAndView mav = new ModelAndView("category/form");
		mav.addObject("category", category);
		return mav;
	}

	@RequestMapping("/insert")
	public ModelAndView insert(Category category) {
		int id = categoryDao.insert(category);
		ModelAndView mav = new ModelAndView(getRedirectUrl("/category/view"));
		mav.addObject("id", id);
		return mav;
	}

	@RequestMapping("/update")
	public ModelAndView update(Category category) {
		categoryDao.update(category);
		ModelAndView mav = new ModelAndView(getRedirectUrl("/category/view"));
		mav.addObject("id", category.getId());
		return mav;
	}
	
	@RequestMapping("/delete")
	public ModelAndView delete(int id) {
		categoryDao.delete(id);
		ModelAndView mav = new ModelAndView(getRedirectUrl("/category/list"));
		return mav;
	}
	
	@RequestMapping("/view")
	public ModelAndView view(int id) {
		Category category = categoryDao.select(id);
		if (category == null) {
			throw new IllegalArgumentException("message");
		}
		ModelAndView mav = new ModelAndView("category/view");
		mav.addObject("category", category);
		return mav;
	}	
	
	@RequestMapping("/list")
	public ModelAndView list(CategoryCondition condition) {
		List<Category> categoryList = categoryDao.search(condition);
		
		ModelAndView mav = new ModelAndView("category/list");
		mav.addObject("condition", condition);
		mav.addObject("categoryList", categoryList);
		return mav;
	}
}